<template>
  <div style="margin: 10px">
    <el-page-header @back="handleBack" content="角色权限配置"></el-page-header>
    <el-card class="form-container" shadow="never">

      <el-tree
          :data="menuTreeList"
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree"
          highlight-current
          :check-strictly="checkStrictly"
          :props="defaultProps">
      </el-tree>
      <div style="margin-top: 20px" align="center">
        <el-button type="warning" @click="handleClear()">清空</el-button>
        <el-button type="primary" @click="handleSelectAll()">全选</el-button>
        <el-button type="success" @click="handleSave()">保存</el-button>
        <el-button type="info" @click="handleBack()">返回</el-button>
      </div>

    </el-card>
  </div>
</template>

<script>
import {sysRoleMenuPostRequire} from '@/api/sysRoleMenu'

export default {
  name: 'allocMenu',
  data() {
    return {
      menuTreeList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      checkStrictly: true,
      roleId: null,
      allMenuIds: []
    }
  },
  created() {
    this.roleId = this.$route.query.roleId
    this.treeList()
    this.getRoleMenu(this.roleId)
  },
  methods: {
    treeList() {
      sysRoleMenuPostRequire('getMenus').then(response => {
        let menuTreeList = response.data
        this.menuTreeList = menuTreeList
        if (menuTreeList != null && menuTreeList.length > 0) {
          for (let i = 0; i < menuTreeList.length; i++) {
            let menu = menuTreeList[i]
            this.allMenuIds.push(menu.id)
          }
        }
      })
    },
    getRoleMenu(roleId) {
      this.checkStrictly = true
      sysRoleMenuPostRequire('getMenusByRoleId', {'roleId': roleId}).then(response => {
        let menuList = response.data
        let checkedMenuIds = []
        if (menuList != null && menuList.length > 0) {
          for (let i = 0; i < menuList.length; i++) {
            let menu = menuList[i]
            checkedMenuIds.push(menu.id)
          }
        }
        var _this = this
        new Promise((resolve) => {
          _this.$refs.tree.setCheckedKeys(checkedMenuIds)
          resolve()
        }).then(() => {
          setTimeout(function () {
            _this.$nextTick(function () {
              _this.checkStrictly = false
            })
          }, 500)
        }).finally()
      })
    },
    handleSave() {
      let checkedNodes = this.$refs.tree.getCheckedNodes()
      let checkedMenuIds = new Set()
      if (checkedNodes != null && checkedNodes.length > 0) {
        for (let i = 0; i < checkedNodes.length; i++) {
          let checkedNode = checkedNodes[i]
          checkedMenuIds.add(checkedNode.id)
          if (checkedNode.parentId) {
            checkedMenuIds.add(checkedNode.parentId)
          }
        }
      }
      this.$confirm('是否分配菜单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {'roleId': this.roleId, 'menuIds': Array.from(checkedMenuIds).toString()}
        sysRoleMenuPostRequire('updateMenusByRoleId', params).then(response => {
          this.$message({
            message: '分配成功',
            type: 'success',
            duration: 1000
          })
          this.$router.back()
        })
      })
    },
    handleClear() {
      this.$refs.tree.setCheckedKeys([])
    },
    handleSelectAll() {
      this.$refs.tree.setCheckedKeys(this.allMenuIds)
    },
    handleBack() {
      this.$router.back()
    }
  }
}
</script>

<style scoped>

</style>
